L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
  // Ported from the OpenLayers implementation. See https://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/Geometry/LinearRing.js#L270
  geodesicArea: function (latLngs) {
    var pointsCount = latLngs.length
    var area = 0.0
    var d2r = (Math.PI / 180)
    var p1; var p2
    //		console.log(pointsCount+"个");
    //		console.log(L.LatLng.DEG_TO_RAD);
    //		console.log(Math.PI/180);
    if (pointsCount > 2) {
      for (var i = 0; i < pointsCount; i++) {
        p1 = latLngs[i]
        p2 = latLngs[(i + 1) % pointsCount]
        area += ((p2.lng - p1.lng) * d2r) *
						(2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r))
      }
      area = area * 6378137.0 * 6378137.0 / 2.0
    }

    return Math.abs(area)
  },

  readableArea: function (area, isMetric) {
    var areaStr

    if (isMetric) {
      if (area >= 1000000) {
        areaStr = (area * 0.000001).toFixed(2) + ' km&sup2'
      } else {
        areaStr = area.toFixed(2) + ' m&sup2'
      }
    } else {
      area *= 0.836127 // Square yards in 1 meter

      if (area >= 3097600) { // 3097600 square yards in 1 square mile
        areaStr = (area / 3097600).toFixed(2) + ' mi&sup2;'
      } else if (area >= 4840) { // 48040 square yards in 1 acre
        areaStr = (area / 4840).toFixed(2) + ' acres'
      } else {
        areaStr = Math.ceil(area) + ' yd&sup2;'
      }
    }

    return areaStr
  },

  readableDistance: function (distance, isMetric) {
    var distanceStr

    if (isMetric) {
      // show metres when distance is < 1km, then show km
      if (distance > 1000) {
        distanceStr = (distance / 1000).toFixed(2) + ' km'
      } else {
        distanceStr = Math.ceil(distance) + ' m'
      }
    } else {
      distance *= 1.09361

      if (distance > 1760) {
        distanceStr = (distance / 1760).toFixed(2) + ' miles'
      } else {
        distanceStr = Math.ceil(distance) + ' yd'
      }
    }

    return distanceStr
  }
})
